package com.xf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xf.common.core.domain.DmpResult;
import com.xf.common.core.entity.dmp.DmpAuthRole;
import com.xf.common.core.page.PageBuilder;
import com.xf.common.core.page.PageQuery;
import com.xf.service.DmpAuthRoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("role")
public class RoleController {
    @Resource
    private DmpAuthRoleService roleService;


    @GetMapping(value = "/getById")
    public DmpResult getById(Long id) {
        DmpAuthRole role = roleService.getById(id);
        return DmpResult.data(role);
    }

    @ApiOperation("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "string")
    })
    @GetMapping(value = "/getByIdWith")
    public DmpResult<DmpAuthRole> getByIdWith(Long id) {
        return DmpResult.data(roleService.getByIdWith(id));
    }

    @ApiOperation("分配菜单和资源")
    @PostMapping(value = "/assign")
    public DmpResult assign(@RequestBody DmpAuthRole role) {
        return roleService.assign(role);
    }

    @ApiOperation("新增角色")
    @PostMapping(value = "/add")
    public DmpResult add(@RequestBody DmpAuthRole role) {
        return roleService.insert(role);
    }

    @ApiOperation("修改角色")
    @PostMapping(value = "/update")
    public DmpResult update(@RequestBody DmpAuthRole role) {
        return roleService.updateByRole(role);
    }

    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "state", value = "角色状态", paramType = "query", dataType = "Integer")
    })
    @GetMapping(value = "/getPage")
    public DmpResult<IPage<DmpAuthRole>> page(PageQuery pageQuery, String roleName, Integer state) {
        return DmpResult.data(roleService.page(PageBuilder.build(pageQuery), roleName, state));
    }

    @ApiOperation("删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "string")
    })
    @PostMapping(value = "/delete")
    public DmpResult delete(@RequestBody DmpAuthRole dmpAuthRole) {
        return roleService.removeByRoleId(dmpAuthRole.getId());
    }

}
